package com.hc.springbootrabbitmq.config;

import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

//消息确认机制检测
@Component
public class MessageConfirmConfig implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnsCallback {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnsCallback(this);
    }

    /**
     *
     * @param correlationData 相关数据
     * @param b 当前消息发送成功或失败
     * @param s 异常原因
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        //消息从生产方发送至交换机则回调
        if(b){
            System.out.println("消息发送至交换机成功");
        }else {
            System.out.println("消息发送至交换机失败");
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        //消息从交换机发送至队列失败则回调
    }

}
